//
//  CommentViewController.m
//  Jingjing
//
//  Created by user on 15/11/11.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "CommentViewController.h"
#import "sys/utsname.h"
#import "RatingBar.h"
#import "LoginViewController.h"
@interface CommentViewController ()<UITextFieldDelegate,UITextViewDelegate,RatingBarDelegate>
{
    UITextView * _textView;
    
    UIScrollView *_scrollView;
    
    UITextField *_placeholderLabel;
    
    NSString * myNewRating;
}

@property(nonatomic,strong)UIButton *sendBtn;
@property (nonatomic,strong) UILabel *mLabel;

@end

@implementation CommentViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    myNewRating = 0;

    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"发表评价"];
    [self.view addSubview:topView];
    
    self.view.backgroundColor = [UIColor hexStringToColor:@"#f3f3f3"];
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 20.0f, 44.0f, 44.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
    
    UIButton * saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-54.0f, 20.0f, 44.0f, 44.0f)];
    [saveBtn setTitle:@"发布" forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    saveBtn.titleLabel.textColor = [UIColor hexStringToColor:@"#FFFFFF"];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:saveBtn];
    
    [self layoutViews];
    
    
}

- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)layoutViews
{
    _scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64.0f, SCREEN_WIDTH, SCREEN_HEIGHT-64.0f)];
    [self.view addSubview:_scrollView];
    
    
    CGFloat height1 = [UILabel height:self.titleStr widthOfFatherView:SCREEN_WIDTH - 32.0f textFont:[UIFont systemFontOfSize:18.0f]];
    UILabel *problemLabel = [[UILabel alloc] initWithFrame:CGRectMake(24.0f, 24.0f, SCREEN_WIDTH - 32.0f, height1)];
    problemLabel.text = self.titleStr;
    problemLabel.numberOfLines = 0;
    problemLabel.textAlignment = NSTextAlignmentLeft;
    problemLabel.font = [UIFont systemFontOfSize:18.0f];
    problemLabel.textColor = kColorTEXT;
    [_scrollView addSubview:problemLabel];
    
    
    //弹出留言框
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(16.0f, NH(problemLabel)+28.0f, SCREEN_WIDTH-32.f, 200.0f)];
    _textView.layer.cornerRadius= 5;
    _textView.layer.masksToBounds = YES;
    
    _textView.textColor= kColorTEXT;
    _textView.font= [UIFont fontWithName:@"Arial"size:16.0];
    _textView.delegate= self;
    _textView.textColor = [UIColor hexStringToColor:@"#000000"];
    _textView.layer.cornerRadius = 2;
    _textView.layer.masksToBounds = YES;
    [_scrollView  addSubview:_textView];
    
    //其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
    _placeholderLabel = [[UITextField alloc]init];
    _placeholderLabel.frame =CGRectMake(5.0f, 8.0f, SCREEN_WIDTH-20.0f, 20.0f);
    _placeholderLabel.placeholder = @"您对这次活动有什么意见可以写在这里哦";//#c2c2c2
    _placeholderLabel.enabled = NO;//lable必须设置为不可用
    _placeholderLabel.font = [UIFont systemFontOfSize:14.0f];
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    [_textView addSubview:_placeholderLabel];
    
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(24.0f, NH(_textView)+24.0f, 80.0f, 30.0f)];
    nameLabel.text = @"我的评分";
    nameLabel.font = [UIFont systemFontOfSize:16.0f];
    nameLabel.textColor = kColorTEXT;
    [_scrollView addSubview:nameLabel];
    
    
    
    RatingBar *ratingBar = [[RatingBar alloc] initWithFrame:CGRectMake(NW(nameLabel)+14.0f,  NH(_textView)+24.0f, 0, 0)];
    //添加到view中
    [_scrollView addSubview:ratingBar];
    //是否是指示器
    ratingBar.isIndicator = NO;
    [ratingBar setImageDeselected:@"icon_star" halfSelected:nil fullSelected:@"icon_star2" andDelegate:self];
    

}

#pragma mark - RatingBar delegate
- (void)ratingChanged:(float)newRating{
    self.mLabel.text = [NSString stringWithFormat:@"评分条的当前结果为:%.0f",newRating*2];
    
    myNewRating =[NSString stringWithFormat:@"%.0f",newRating*2] ;
}


//提交按钮
-(void)saveBtnClick
{
    
    if (_textView.text.length>200) {
        ALERT_VIEW(@"反馈意见最多只能输入200个字符");
        return;
    }
    
    if (myNewRating== 0) {
        ALERT_VIEW(@"请选择评分哦");
        return;
    }
    
    
    [[AnimationIndicator sharedIndicator] startAnimationWithLoadText:@"加载中"];
    [AppGlobalClass PostDataWithNSstring:USERGOTOSCORE withDictionary:@{@"takePartId":self.takePartId,@"score":myNewRating,@"evaluate":_textView.text,@"token":[[NSUserDefaults standardUserDefaults]objectForKey:@"token"]} success:^(NSDictionary *dictionary) {
        
        
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {

            
            [self.navigationController popViewControllerAnimated:YES];
            ALERT_VIEW(@"评分成功");
            
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            
            ALERT_VIEW(str);
            
        }
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
        
    } failure:^(NSError *error) {
        
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    } networkerror:^(NSString *networkerror) {
        
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    }];
    
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [_textView resignFirstResponder];
    return YES;
}


#pragma mark-关闭键盘(TextView) 换行时。隐藏键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView
{
    
    if (textView.text.length == 0) {
        _placeholderLabel.placeholder = @"您对这次活动有什么意见可以写在这里哦";
        
        

    }else{
        _placeholderLabel.placeholder = @"";

        }
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardShow:)
                                                 name: UIKeyboardDidShowNotification object:nil];
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardHide:)
                                                 name: UIKeyboardDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloginView) name:@"reLogin" object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)reloginView
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号在另一台机器登录，请重新登录!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}
#pragma mark------------alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //删除token等用户数据
    [AccountTools exitLogin];
    LoginViewController *loginViewCon = [[LoginViewController alloc]init];
    loginViewCon.backBtHide = FALSE;
    [self presentViewController:loginViewCon animated:TRUE completion:^{
        //回到哪个界面
    }];
    
}


//键盘下降
- (void)keyboardHide:(NSNotification *)noti
{
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    [UIView setAnimationCurve:7];
    _scrollView.contentOffset = CGPointMake(0, 0);
    [UIView commitAnimations];
}

//键盘升起
- (void)keyboardShow:(NSNotification *)noti
{
    NSValue *value = [noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [value CGRectValue];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
    [UIView setAnimationCurve:7];
    if (keyboardFrame.origin.y != SCREEN_HEIGHT) {
        CGFloat a = _scrollView.frame.size.height + _scrollView.frame.origin.y+80.0f;
        if (a < keyboardFrame.origin.y) {
            _scrollView.contentOffset = CGPointMake(0, 0);
        }else
        {
            _scrollView.contentOffset = CGPointMake(0, 100);
        }
        
    }
    
    [UIView commitAnimations];
    
}

@end
